package com.anjubao.parking.municipal.parkinglot.service;

import com.anjubao.parking.municipal.common.utils.BeanUtils;
import com.anjubao.parking.municipal.parkinglot.config.ConfigCenterConstant;
import com.anjubao.parking.municipal.parkinglot.dto.ParkingLotDeviceStatusDto;
import com.anjubao.parking.municipal.parkinglot.mapper.ParkingLotDeviceStatusMapper;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDeviceStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备状态表Service
 *
 * @author zhuchunfeng
 */
@Service
public class ParkingLotDeviceStatusService extends ServiceImpl<ParkingLotDeviceStatusMapper, ParkingLotDeviceStatus>{

    @Resource
    private ConfigCenterConstant configCenterConstant;
    @Resource
    private ParkingLotDeviceStatusMapper parkingLotDeviceStatusMapper;

    /**
     * 设备运行情况列表
     * @param page 页码
     * @param pageSize 页大小
     * @param cityId 城市ID
     * @return 设备运行情况列表
     */
    public IPage<ParkingLotDeviceStatusDto> listDevicesStatus(Integer page,Integer pageSize , Integer cityId) {
        IPage<ParkingLotDeviceStatus> iPage = new Page<>(page, pageSize);
        QueryWrapper<ParkingLotDeviceStatus> queryWrapper = new QueryWrapper<>(new ParkingLotDeviceStatus()
                .setCityId(cityId)
                .setStatus(1)
        );
        queryWrapper.orderByDesc("last_report_time");
        return parkingLotDeviceStatusMapper.selectPage(iPage, queryWrapper).convert(t -> BeanUtils.transform(ParkingLotDeviceStatusDto.class, t));
    }

    /**
     * 设备故障列表
     * @param size 列表大小
     * @param cityId 城市ID
     * @return 设备故障列表
     */
    public List<ParkingLotDeviceStatusDto> listFailureDevice(Integer size, Integer cityId) {
        Integer lowBatteryThreshold = configCenterConstant.getMonitorLowBatteryThreshold();
        List<ParkingLotDeviceStatusDto> dtoList = parkingLotDeviceStatusMapper.queryFailureDevice(size, cityId,lowBatteryThreshold);
        for (ParkingLotDeviceStatusDto dto : dtoList) {
            if (dto.getStatus() == -1) {
                dto.setReportMsg("故障异常");
            }else {
                dto.setReportMsg("电量不足");
            }
        }
        return dtoList;
    }
}
